//
//  BJFollowListViewController.m
//  BowJoy
//
//  Created by  on 2024/5/14.
//

#import "BJFollowListViewController.h"
#import "BJFolloewListTableViewCell.h"

@interface BJFollowListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic) NSMutableArray<BJFollowedUserModelData *> *dataList;

@end

@implementation BJFollowListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviTitle = @"Follow List";
     

    
    self.view.addChild(self.tableView);
    self.tableView.makeCons(^{
        make.leading.trailing.constants(0);
        make.bottom.view(self.view).bottom.constants(-90);
        make.top.view(self.naviContentView).bottom.constants(0);
    });

    [self getData];

    // Do any additional setup after loading the view.
}
- (void)getData{
    kWeakSelf(weakSelf)
    [[BJNetManager shareNetManager] postGetFriendListPageWithlimit:20 completionHandler:^(BJFollowedUserModel * _Nonnull model, NSError * _Nonnull error) {
        if(!error){
            [weakSelf.dataList removeAllObjects];
            [weakSelf.dataList addObjectsFromArray:model.data];
         }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView endHeaderRefresh];
    }];

}
- (NSMutableArray<BJFollowedUserModelData *> *)dataList{
    AsGetMethodReturnObjc(_dataList);
    _dataList = [NSMutableArray array];
    return _dataList;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BJFolloewListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJFolloewListTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.blockBtn setImage:[UIImage imageNamed:@"icon_follewList_btn"] forState:UIControlStateNormal];
    if(self.dataList.count>indexPath.item){
        BJFollowedUserModelData *model = self.dataList[indexPath.row];
        cell.nikeNameL.text = model.nickname;
        [cell.headerIcon setImageURL:[NSURL URLWithString:model.avatarUrl]];
    }
    kWeakSelf(weakSelf)
    cell.blockBtn.onClick(^{
        BJFollowedUserModelData *model = self.dataList[indexPath.row];
        [[BJNetManager shareNetManager]postDeleteFriendWithFollowUserId:model.userId completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            NSDictionary *dict = responOj;
            NSInteger code = [dict[@"code"]integerValue];
            if(!error && code == 0){
                [BJUserManager sharedManager].followingNum --;

                [ASHUD showWithText:@"Unfollow Successfully" afterDelay:2];
                [[BJFMDBVoiceManager sharedManager] uptadeDataWithFrend:0 andUserId:model.userId];
                [[BJFMDBManager sharedManager] uptadeDataWithFrend:0 andUserId:model.userId];


                [weakSelf getData];
            }
        }];

    });
    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController popViewControllerAnimated:YES];
}


- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerNib:[UINib nibWithNibName:@"BJFolloewListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BJFolloewListTableViewCell"];
    }
    return _tableView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
